package cn.chencaiju.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.chencaiju.domain.Post;

@Controller
public class PostController {
	
	@RequestMapping("/pages/post")
	@ResponseBody
	public Post One(int id) {
		Post p=new Post(id);
		System.out.println(p);
		
		return p;
	}
	@RequestMapping("/pages/what")
	@ResponseBody
	public List<Post> list(){
		List<Post> ps=loadPosts();
		return ps;
	}
	@RequestMapping("/pages/posts")
	public ModelAndView load(Model model) {
		List<Post> post=loadPosts();
		ModelAndView mv=new ModelAndView();
		mv.setViewName("posts");
		model.addAttribute("posts",post);
		return mv;
	}


	private List<Post> loadPosts() {
		List<Post> posts=new ArrayList<Post>();
		for(int i=0;i<10;i++) {
			Post p=new Post(i);
			posts.add(p);
		}
		return posts;
	}
}
